package com.customization.unionpay.market.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.CarryoverOfUnverifiedPrepaymentsService;
import com.customization.unionpay.market.util.CarryoverOfUnverifiedButton;
import com.engine.core.impl.Service;
import weaver.conn.RecordSetTrans;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class CarryoverOfUnverifiedPrepaymentsServiceImpl extends Service implements CarryoverOfUnverifiedPrepaymentsService {

    private static final Logger log = LoggerFactory.getLogger(CarryoverOfUnverifiedPrepaymentsServiceImpl.class);

    @Override
    public JSONObject carryoverOfUnverifiedSelectByDate(User user,String dateMonth) throws Exception {

        log.info("dateMonth："+dateMonth);

        JSONObject result = new JSONObject();

        // 营销活动日报表台账表单id
        int reportFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_activity_daily_report_form_id"));
        // 营销活动事项台账表单id
        int ledgerFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_activity_ledger_form_id"));

        // 查询枚举 日报流水类型 正常数据
        String selectValueNormalData = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "rblslx", "正常数据");
        log.info("正常数据枚举值："+selectValueNormalData);
        // 查询枚举 日报流水类型 核定金额变更数据
        String selectValueApprovedChangeData = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "rblslx", "核定金额变更数据");
        log.info("核定金额变更数据枚举值："+selectValueApprovedChangeData);

        String isNot = com.customization.unionpay.market.util.FormDataUtils.getItemValue(ledgerFormId, "cwsfjs", "否");
        log.info("财务是否结项："+isNot);

        // 未核销预付款结转台账台账表单id
        int prepaymentsFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_form_id"));
        // 未核销预付款结转台账台账模块id
        int prepaymentsFormmodeId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_formmode_id"));
        // 未核销预付款结转台账明细台账表单id
        int prepaymentsDetailsFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_details_form_id"));
        // 未核销预付款结转台账明细台账模块id
        int prepaymentsDetailsFormmodeId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_details_formmode_id"));

        // 获取营销活动日报表台账主表表名
        String reportFormTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(reportFormId);
        log.info("获取营销活动日报表台账主表表名："+reportFormTableName);
        // 营销活动事项台账主表表名
        String ledgerTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(ledgerFormId);
        log.info("营销活动事项台账主表表名："+ledgerTableName);
        // 未核销预付款结转台账
        String prepaymentsTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(prepaymentsFormId);
        log.info("未核销预付款结转台账："+prepaymentsTableName);
        // 未核销预付款结转明细台账
        String prepaymentsDetailsTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(prepaymentsDetailsFormId);
        log.info("未核销预付款结转明细台账："+prepaymentsDetailsTableName);
        if(StrUtil.isNotBlank(reportFormTableName)) {
            if(StrUtil.isNotBlank(ledgerTableName)) {
                if(StrUtil.isNotBlank(prepaymentsTableName)) {
                    if(StrUtil.isNotBlank(prepaymentsDetailsTableName)) {
                        // 未核销预付款结转台账  // 已结算 不接受查询  已有月份 未结算 更新台账 更新中间表
                        List<JSONObject> prepaymentsList = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.selectCarryoverOfUnverifiedBySth(prepaymentsTableName, dateMonth);

                        if(prepaymentsList.size() > 0){
                            JSONObject prepayments = prepaymentsList.get(0);
                            String id = prepayments.getString("id");
                            String jsrq = prepayments.getString("jsrq");
                            if(jsrq == null || "".equals(jsrq)){
                                // 修改
                                List<JSONObject> rzrqLists = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.selectVerificationReimbursementByRzrq(dateMonth,ledgerTableName);
                                log.info("update-data:"+rzrqLists);
                                if(rzrqLists.size() > 0) {
                                    log.info("in-update-rzrqLists");
                                    // 营销事项申请单号合集
                                    List<String> codeList = new ArrayList<>();
                                    // 入账金额合集
                                    List<String> rzjeList = new ArrayList<>();
                                    // 核销金额合集
                                    List<String> hxjeList = new ArrayList<>();
                                    // 以营销事项申请单号为唯一值的组合数据
                                    JSONObject singleCode = new JSONObject();

                                    // 营销平台事项申请单号
                                    String sqdh = "";
                                    // 营销平台活动授权码
                                    String yxpthdsqm = "";
                                    // 财务是否结束
                                    String cwsfjs = "";
                                    // 公司
                                    String gs = "";
                                    // 部门
                                    String bm = "";
                                    // 是否为大协议
                                    String sfwdxy = "";
                                    // 合同编号
                                    String htbh = "";
                                    // 活动类型
                                    String actTp = "";
                                    // 资金来源
                                    String zjly = "";
                                    // 已入账金额
                                    Double yrzje = null;
                                    // 入账批次号
                                    String rzpch = "";

                                    for (JSONObject rzrqList : rzrqLists) {
                                        // 供应商编号
                                        String gysbh = rzrqList.getString("gysbh");
                                        // 营销事项申请单号
                                        String ysxsqd = rzrqList.getString("ysxsqd");
                                        // 本次核销金额
                                        Double bchxje = rzrqList.getDouble("bchxje");
                                        // 经济事项
                                        String jjsx = rzrqList.getString("jjsx");

                                        chooseDataByOnlyValue(singleCode, ysxsqd, bchxje, gysbh, jjsx, yxpthdsqm, cwsfjs, gs, bm, sfwdxy, htbh, actTp, zjly, yrzje, rzpch);

                                        if (!"".equals(ysxsqd)) {
                                            if (!codeList.contains(ysxsqd)) {
                                                codeList.add(ysxsqd);
                                            }
                                        }
                                        hxjeList.add(String.valueOf(bchxje));
                                    }
                                    log.info("营销事项申请单号:"+codeList);
                                    boolean flag = false;
                                    for (String codeStr : codeList) {
                                        List<JSONObject> yxxgList = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.selectVerificationReimbursementBySth(ledgerTableName, reportFormTableName, codeStr, selectValueNormalData, selectValueApprovedChangeData,isNot);
                                        log.info("yxxgList:"+yxxgList);
                                        if(yxxgList.size() > 0) {
                                            // 营销事项申请单号
                                            for (JSONObject json : yxxgList) {
                                                // 营销平台事项申请单号
                                                sqdh = json.getString("sqdh");
                                                // 营销平台活动授权码
                                                yxpthdsqm = json.getString("yxpthdsqm");
                                                // 财务是否结束
                                                cwsfjs = json.getString("cwsfjs");
                                                // 公司
                                                gs = json.getString("gs");
                                                // 部门
                                                bm = json.getString("bm");
                                                // 是否为大协议
                                                sfwdxy = json.getString("sfwdxy");
                                                // 合同编号
                                                htbh = json.getString("htbh");
                                                // 活动类型
                                                actTp = json.getString("ACT_TP");
                                                // 资金来源
                                                zjly = json.getString("zjly");
                                                // 已入账金额
                                                yrzje = json.getDouble("yrzje");
                                                // 入账批次号
                                                rzpch = json.getString("rzpch");

                                                chooseDataByOnlyValue(singleCode, sqdh, 0D, "", "", yxpthdsqm, cwsfjs, gs, bm, sfwdxy, htbh, actTp, zjly, yrzje, rzpch);
                                                if(yrzje != null) {
                                                    rzjeList.add(String.valueOf(yrzje));
                                                }
                                            }
                                        }else {
                                            flag = true;
                                        }
                                    }
                                    if(flag){
                                        result.put("msg", "查询该月活动事项台账数据为空！");
                                        result.put("data", null);
                                        result.put("code", 500);
                                        result.put("errorCode", 10003);
                                    }else {
                                        log.info("拼接参数:" + singleCode);

                                        String rzjeAllStr = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.sumStrNumber(rzjeList);
                                        log.info("入账总金额:" + rzjeAllStr);

                                        String hxjeAllStr = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.sumStrNumber(hxjeList);
                                        log.info("核销总金额:" + hxjeAllStr);

                                        for (Object key : singleCode.keySet()) {
                                            String yxsxsqdhDt = String.valueOf(key);
                                            log.info("yxsxsqdhDt:" + yxsxsqdhDt);

                                            JSONObject singleCodeJSONObject = singleCode.getJSONObject(yxsxsqdhDt);
                                            log.info("singleCodeJSONObject:" + singleCodeJSONObject);

                                            String bchxjeDt = singleCodeJSONObject.getString("bchxje") == null ? "0": ("".equals(singleCodeJSONObject.getString("bchxje")) ? "0" :singleCodeJSONObject.getString("bchxje"));
                                            String gysbhDt = singleCodeJSONObject.getString("gysbh");
                                            String jjsxDt = singleCodeJSONObject.getString("jjsx");
                                            String yxpthdsqmDt = singleCodeJSONObject.getString("yxpthdsqm");
                                            String cwsfjsDt = singleCodeJSONObject.getString("cwsfjs");
                                            String gsDt = singleCodeJSONObject.getString("gs");
                                            String bmDt = singleCodeJSONObject.getString("bm");
                                            String sfwdxyDt = singleCodeJSONObject.getString("sfwdxy");
                                            String htbhDt = singleCodeJSONObject.getString("htbh");
                                            String actTpDt = singleCodeJSONObject.getString("actTp");
                                            String zjlyDt = singleCodeJSONObject.getString("zjly");
                                            String yrzjeDt = singleCodeJSONObject.getString("yrzje") == null ? "0":("".equals(singleCodeJSONObject.getString("yrzje")) ? "0":singleCodeJSONObject.getString("yrzje"));
                                            String rzpchDt = singleCodeJSONObject.getString("rzpch");

                                            String dhxjeStrDt = String.valueOf(Double.parseDouble(yrzjeDt) - Double.parseDouble(bchxjeDt));
                                            log.info("待核销明细金额:" + dhxjeStrDt);
                                            List<JSONObject> dtList = com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.selectCarryoverOfUnverifiedDetailsBySth(prepaymentsDetailsTableName, yxsxsqdhDt, yxpthdsqmDt);
                                            if (dtList.size() > 0) {
                                                String idDt = dtList.get(0).getString("id");
                                                // 修改明细数据
                                                log.info("修改明细数据");
                                                com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.updateCarryoverOfUnverifiedDetailsById(prepaymentsDetailsTableName, idDt, dateMonth, yxsxsqdhDt, yxpthdsqmDt, cwsfjsDt, gsDt, bmDt,
                                                        sfwdxyDt, jjsxDt, gysbhDt, htbhDt, zjlyDt, actTpDt, yrzjeDt,
                                                        bchxjeDt, dhxjeStrDt, rzpchDt, "", "", "", "");
                                            } else {
                                                // 添加明细数据
                                                log.info("添加明细数据");
                                                com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.addCarryoverOfUnverifiedDetailsByFormId(prepaymentsDetailsFormId, prepaymentsDetailsFormmodeId, prepaymentsDetailsTableName, user, dateMonth, yxsxsqdhDt, yxpthdsqmDt, cwsfjsDt, gsDt, bmDt,
                                                        sfwdxyDt, jjsxDt, gysbhDt, htbhDt, zjlyDt, actTpDt, yrzjeDt,
                                                        bchxjeDt, dhxjeStrDt, rzpchDt, "", "", "", "");
                                            }

                                        }
                                        String dhxjeStr = String.valueOf(Double.parseDouble(rzjeAllStr) - Double.parseDouble(hxjeAllStr));
                                        log.info("待核销总金额:" + dhxjeStr);
//                                        String itemValue = FormDataUtils.getItemValue(prepaymentsFormId, "zt", "未结转");
//                                        log.info("状态 未结转：" + itemValue);
                                        // 已入账金额 根据月份查询核销报账单的明细中的营销事项申请单号查询活动事项台账中【是否结项】=“否”的已入账金额的汇总
                                        // 已核销金额 根据月份查询核销报账单的本次已核销金额汇总
                                        // 待核销金额 已入账金额-已核销金额
                                        // 状态 未结转/已结转/已冲销 【查询】【结转】【冲销】
                                        com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.updateCarryoverOfUnverifiedById(prepaymentsTableName, id, rzjeAllStr, hxjeAllStr, dhxjeStr, "", "", "未结转");
                                    }
                                }else {
                                    result.put("msg", "查询该月核销报账流程数据为空！");
                                    result.put("data", null);
                                    result.put("code", 500);
                                    result.put("errorCode", 10003);
                                }
                            }else {
                                result.put("msg", "该月未核销预付款结转已结算！");
                                result.put("data", null);
                                result.put("code", 500);
                                result.put("errorCode", 10003);
                            }
                        }else {
                            // 不存在 添加
                            List<JSONObject> rzrqLists = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.selectVerificationReimbursementByRzrq(dateMonth,ledgerTableName);
                            log.info("add-rzrqLists："+rzrqLists);
                            if(rzrqLists.size() > 0) {
                                log.info("in-add-rzrqLists");
                                // 营销事项申请单号合集
                                List<String> codeList = new ArrayList<>();
                                // 入账金额合集
                                List<String> rzjeList = new ArrayList<>();
                                // 核销金额合集
                                List<String> hxjeList = new ArrayList<>();
                                // 以营销事项申请单号为唯一值的组合数据
                                JSONObject singleCode = new JSONObject();

                                // 营销平台事项申请单号
                                String sqdh = "";
                                // 营销平台活动授权码
                                String yxpthdsqm = "";
                                // 财务是否结束
                                String cwsfjs = "";
                                // 公司
                                String gs = "";
                                // 部门
                                String bm = "";
                                // 是否为大协议
                                String sfwdxy = "";
                                // 合同编号
                                String htbh = "";
                                // 活动类型
                                String actTp = "";
                                // 资金来源
                                String zjly = "";
                                // 已入账金额
                                Double yrzje = null;
                                // 入账批次号
                                String rzpch = "";

                                for (JSONObject rzrqList : rzrqLists) {
                                    // 供应商编号
                                    String gysbh = rzrqList.getString("gysbh");
                                    // 营销事项申请单号
                                    String ysxsqd = rzrqList.getString("ysxsqd");
                                    // 本次核销金额
                                    Double bchxje = rzrqList.getDouble("bchxje");
                                    // 经济事项
                                    String jjsx = rzrqList.getString("jjsx");

                                    if (!"".equals(ysxsqd)) {
                                        chooseDataByOnlyValue(singleCode, ysxsqd, bchxje, gysbh, jjsx, yxpthdsqm, cwsfjs, gs, bm, sfwdxy, htbh, actTp, zjly, yrzje, rzpch);
                                        if (!codeList.contains(ysxsqd)) {
                                            codeList.add(ysxsqd);
                                        }
                                    }
                                    if (bchxje != null) {
                                        hxjeList.add(String.valueOf(bchxje));
                                    }
                                }

                                log.info("营销事项申请单号:"+codeList);
                                boolean flag = false;
                                for (String codeStr : codeList) {
                                    List<JSONObject> yxxgList = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.selectVerificationReimbursementBySth(ledgerTableName, reportFormTableName, codeStr, selectValueNormalData, selectValueApprovedChangeData,isNot);
                                    log.info("yxxgList:"+yxxgList);
                                    if(yxxgList.size() > 0) {
                                        // 营销事项申请单号
                                        for (JSONObject json : yxxgList) {
                                            // 营销平台事项申请单号
                                            sqdh = json.getString("sqdh");
                                            // 营销平台活动授权码
                                            yxpthdsqm = json.getString("yxpthdsqm");
                                            // 财务是否结束
                                            cwsfjs = json.getString("cwsfjs");
                                            // 公司
                                            gs = json.getString("gs");
                                            // 部门
                                            bm = json.getString("bm");
                                            // 是否为大协议
                                            sfwdxy = json.getString("sfwdxy");
                                            // 合同编号
                                            htbh = json.getString("htbh");
                                            // 活动类型
                                            actTp = json.getString("ACT_TP");
                                            // 资金来源
                                            zjly = json.getString("zjly");
                                            // 已入账金额
                                            yrzje = json.getDouble("yrzje");
                                            // 入账批次号
                                            rzpch = json.getString("rzpch");

                                            chooseDataByOnlyValue(singleCode, sqdh, 0D, "", "", yxpthdsqm, cwsfjs, gs, bm, sfwdxy, htbh, actTp, zjly, yrzje, rzpch);
                                            if(yrzje != null) {
                                                rzjeList.add(String.valueOf(yrzje));
                                            }
                                        }
                                    }else {
                                        flag = true;
                                    }
                                }
                                if(flag){
                                    result.put("msg", "查询该月活动事项台账数据为空！");
                                    result.put("data", null);
                                    result.put("code", 500);
                                    result.put("errorCode", 10003);
                                }else {

                                    log.info("拼接参数:" + singleCode);

                                    String rzjeAllStr = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.sumStrNumber(rzjeList);
                                    log.info("入账总金额:" + rzjeAllStr);

                                    String hxjeAllStr = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.sumStrNumber(hxjeList);
                                    log.info("核销总金额:" + hxjeAllStr);

                                    for (Object key : singleCode.keySet()) {
                                        String yxsxsqdhDt = String.valueOf(key);
                                        log.info("yxsxsqdhDt:" + yxsxsqdhDt);
                                        JSONObject singleCodeJSONObject = singleCode.getJSONObject(yxsxsqdhDt);
                                        log.info("singleCodeJSONObject:" + singleCodeJSONObject);
                                        String bchxjeDt = singleCodeJSONObject.getString("bchxje") == null ? "0": ("".equals(singleCodeJSONObject.getString("bchxje")) ? "0" :singleCodeJSONObject.getString("bchxje"));
                                        String gysbhDt = singleCodeJSONObject.getString("gysbh");
                                        String jjsxDt = singleCodeJSONObject.getString("jjsx");
                                        String yxpthdsqmDt = singleCodeJSONObject.getString("yxpthdsqm");
                                        String cwsfjsDt = singleCodeJSONObject.getString("cwsfjs");
                                        String gsDt = singleCodeJSONObject.getString("gs");
                                        String bmDt = singleCodeJSONObject.getString("bm");
                                        String sfwdxyDt = singleCodeJSONObject.getString("sfwdxy");
                                        String htbhDt = singleCodeJSONObject.getString("htbh");
                                        String actTpDt = singleCodeJSONObject.getString("actTp");
                                        String zjlyDt = singleCodeJSONObject.getString("zjly");
                                        String yrzjeDt = singleCodeJSONObject.getString("yrzje") == null ? "0":("".equals(singleCodeJSONObject.getString("yrzje")) ? "0":singleCodeJSONObject.getString("yrzje"));
                                        String rzpchDt = singleCodeJSONObject.getString("rzpch");

                                        String dhxjeStrDt = String.valueOf(Double.parseDouble(yrzjeDt) - Double.parseDouble(bchxjeDt));
                                        log.info("待核销明细金额:" + dhxjeStrDt);
                                        log.info("添加明细数据");
                                        com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.addCarryoverOfUnverifiedDetailsByFormId(prepaymentsDetailsFormId, prepaymentsDetailsFormmodeId, prepaymentsDetailsTableName, user, dateMonth, yxsxsqdhDt, yxpthdsqmDt, cwsfjsDt, gsDt, bmDt,
                                                sfwdxyDt, jjsxDt, gysbhDt, htbhDt, zjlyDt, actTpDt, yrzjeDt,
                                                bchxjeDt, dhxjeStrDt, rzpchDt, "", "", "", "");
                                    }
                                    String dhxjeStr = String.valueOf(Double.parseDouble(rzjeAllStr) - Double.parseDouble(hxjeAllStr));
                                    log.info("待核销总金额:" + dhxjeStr);
//                                    String itemValue = FormDataUtils.getItemValue(prepaymentsFormId, "zt", "未结转");
//                                    log.info("状态 未结转：" + itemValue);
                                    // 已入账金额 根据月份查询核销报账单的明细中的营销事项申请单号查询活动事项台账中【是否结项】=“否”的已入账金额的汇总
                                    // 已核销金额 根据月份查询核销报账单的本次已核销金额汇总
                                    // 待核销金额 已入账金额-已核销金额
                                    // 状态 未结转/已结转/已冲销 【查询】【结转】【冲销】
                                    com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.addCarryoverOfUnverifiedByFormId(prepaymentsFormId, prepaymentsFormmodeId, prepaymentsTableName, user, dateMonth, rzjeAllStr, hxjeAllStr, dhxjeStr, "", "", "未结转");
                                }
                            }else {
                                result.put("msg", "查询该月核销报账流程数据为空！");
                                result.put("data", null);
                                result.put("code", 500);
                                result.put("errorCode", 10003);
                            }
                        }
                    }else {
                        result.put("msg", "获取未核销预付款结转明细台账为空！");
                        result.put("data", null);
                        result.put("code", 500);
                        result.put("errorCode", 10003);
                    }
                }else {
                    result.put("msg", "获取未核销预付款结转台账为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                }
            }else {
                result.put("msg", "获取营销活动事项台账主表表名为空！");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        }else {
            result.put("msg", "获取营销活动日报表台账主表表名为空！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 10003);
        }
        return result;
    }

    @Override
    public String carryoverOfUnverifiedWriteOff(String ids, User user) {
        // 冲销
        String result = null;
        try {
            ResourceComInfo rci = new ResourceComInfo();
            DepartmentComInfo dci = new DepartmentComInfo();
            SubCompanyComInfo scci = new SubCompanyComInfo();

            // 未核销预付款结转台账台账表单id
            int prepaymentsFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_form_id"));
            // 未核销预付款结转台账明细台账表单id
            int prepaymentsDetailsFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_details_form_id"));

            // 会计引擎中间表表单id
            int kjyqFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "kjyq_form_id"));
            // 会计引擎中间表模块id
            int kjyqFormmodeId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "kjyq_formmode_id"));

            // # 营销事项申请流程id
            String marketingApplicationWorkId = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_application_work_id");
            String workFlowTableName = com.customization.unionpay.market.util.FormDataUtils.getWorkFlowTableName(marketingApplicationWorkId);
            log.info("申请单主表表名：" + workFlowTableName);

            // 未核销预付款结转台账
            String prepaymentsTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(prepaymentsFormId);
            log.info("未核销预付款结转台账："+prepaymentsTableName);
            // 未核销预付款结转明细台账
            String prepaymentsDetailsTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(prepaymentsDetailsFormId);
            log.info("未核销预付款结转明细台账："+prepaymentsDetailsTableName);
            // 会计引擎中间表台账
            String kjyqTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(kjyqFormId);
            log.info("会计引擎中间表台账："+kjyqTableName);

            int userUID = user.getUID();

            if(StrUtil.isNotBlank(prepaymentsTableName)) {
                if(StrUtil.isNotBlank(prepaymentsDetailsTableName)) {
                    String useId = String.valueOf(userUID);

                    String workCode = rci.getWorkcode(useId);
                    log.info("workCode："+workCode);

                    String departmentId = rci.getDepartmentID(useId);
                    String departmentCode = dci.getDepartmentCode(departmentId);
                    log.info("departmentCode："+departmentCode);

                    String subCompanyId = rci.getSubCompanyID(useId);
                    String subCompanyCode = scci.getSubCompanyCode(subCompanyId);
                    log.info("subCompanyCode："+subCompanyCode);

                    String personId = com.customization.unionpay.market.util.FormDataUtils.getPersonByRoleName("营销-财务主管");
                    log.info("personId："+personId);

                    Date date = new Date();

                    String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
                    List<JSONObject> carryoverDatas = com.customization.unionpay.market.util.CarryoverOfUnverifiedButton.selectCarryoverOfUnverifiedByIds(prepaymentsTableName, ids,"cxrq");
                    log.info("查询cxrq数据："+carryoverDatas);

                    if(carryoverDatas.size() > 0){
                        for (JSONObject carryoverData : carryoverDatas) {
                            String id = carryoverData.getString("id");
                            String rq = carryoverData.getString("rq");

                            String cxpch = UUID.randomUUID().toString();
                            log.info("冲销批次号："+cxpch);

                            // 获取选择期间的明细数据
                            List<JSONObject> carryoverDetailsDatas = com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.selectCarryoverOfUnverifiedDetailsByQJ(prepaymentsDetailsTableName, rq);
                            log.info("查询rq明细数据："+carryoverDetailsDatas);

                            if(carryoverDetailsDatas.size() > 0) {
                                List<JSONObject> detailsList = new ArrayList<>();
                                JSONObject mainParams = new JSONObject();
                                mainParams.put("lydjlx","未核销预付款结转台账冲销");
                                Boolean isFlag = true;

                                for (JSONObject carryoverDetailsData : carryoverDetailsDatas) {
                                    String yxsxsqdh = carryoverDetailsData.getString("yxsxsqdh");
                                    log.info("yxsxsqdh："+yxsxsqdh);
                                    // 查询营销事项申请单
                                    JSONObject marketingApplicationInfo = com.customization.unionpay.market.util.FormDataUtils.getMarketingApplicationInfo(yxsxsqdh, workFlowTableName);
                                    log.info("查询营销事项申请单数据："+marketingApplicationInfo);
                                    if(marketingApplicationInfo != null){
                                        JSONObject params = new JSONObject();

                                        String detailsId = carryoverDetailsData.getString("id");
                                        com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.updatePchById(detailsId,cxpch,"cxpch",prepaymentsDetailsTableName);

                                        String yxpthdsqm = carryoverDetailsData.getString("yxpthdsqm");
                                        String cwsfjs = carryoverDetailsData.getString("cwsfjs");
                                        String sfwdxy = carryoverDetailsData.getString("sfwdxy");
                                        String jjsx = carryoverDetailsData.getString("jjsx");
                                        String gysbh = carryoverDetailsData.getString("gysbh");
                                        String htbh = carryoverDetailsData.getString("htbh");
                                        String zjly = carryoverDetailsData.getString("zjly");
                                        String actTp = carryoverDetailsData.getString("ACT_TP");
                                        String yrzje = carryoverDetailsData.getString("yrzje");
                                        String yhxje = carryoverDetailsData.getString("yhxje");

                                        // 核算主体			    (单号查)   zth          法人公司	        核算主体编码	hsztbm	核算主体
                                        String zth = marketingApplicationInfo.getString("zth");
                                        Object hszt = mainParams.get("hszt");
                                        if(hszt == null  || "".equals(hszt)){
                                            mainParams.put("hszt",zth);
                                        }
                                        // nowDate 结算日期	     		jsrq		           凭证日期、头文本	入账日期	    rzrq	入账日期
                                        Object rzrq = mainParams.get("rzrq");
                                        if(rzrq == null  || "".equals(rzrq)){
                                            mainParams.put("rzrq",nowDate);
                                        }
                                        // cxpzh 冲销批次号	cxpzh		           备选参考编号	    表单号	    bdh	    表单号
                                        Object bdh = mainParams.get("bdh");
                                        if(bdh == null  || "".equals(bdh)){
                                            mainParams.put("bdh",cxpch);
                                        }
                                        // useId          凭证制单人				触发【核算入账】的用户ID	凭证制单人	    凭证制单人	zhidanren	凭证制单人
                                        Object pzzdr = mainParams.get("pzzdr");
                                        if(pzzdr == null  || "".equals(pzzdr)){
                                            mainParams.put("pzzdr",useId);
                                        }
                                        // personId       凭证复核人				财务主管	                凭证复核人	    凭证复核人	fuheren	    凭证复核人
                                        Object pzfhr = mainParams.get("pzfhr");
                                        if(pzfhr == null  || "".equals(pzfhr)){
                                            mainParams.put("pzfhr",personId);
                                        }
                                        // workCode       经办人					       经办人（不传浪潮，返回到表单凭证明细）	    经办人	    jbr	    经办人
                                        Object jbr = mainParams.get("jbr");
                                        if(jbr == null  || "".equals(jbr)){
                                            mainParams.put("jbr",useId);
                                        }
                                        // departmentCode 部门	        		 		   经办人部门（不传浪潮，返回到表单凭证明细）	经办人部门	jbrbm	经办人部门
                                        Object jbrbm = mainParams.get("jbrbm");
                                        if(jbrbm == null  || "".equals(jbrbm)){
                                            mainParams.put("jbrbm",departmentId);
                                        }
                                        // subCompanyCode 公司	        		 		   经办人公司（不传浪潮，返回到表单凭证明细）	经办人公司	jbrgs	经办人公司
                                        Object jbrgs = mainParams.get("jbrgs");
                                        if(jbrgs == null  || "".equals(jbrgs)){
                                            mainParams.put("jbrgs",subCompanyId);
                                        }
//                                        // 经济事项	    		jjsx 					取编码	映射表	经济事项编码	            jjsxbm	经济事项
//                                        JSONObject economicMattersInfo = FormDataUtils.getEconomicMattersInfo(jjsx);
//                                        String economicCode = economicMattersInfo.getString("code");
                                        // 活动类型	    		ACT_TP（查表）					取编码	活动类型、映射表字段	活动类型编码	hdlxbm	活动类型
                                        String actTpName = com.customization.unionpay.market.util.FormDataUtils.getItemName(prepaymentsDetailsFormId, "ACT_TP", actTp);
                                        String actTpCode = com.customization.unionpay.market.util.FormDataUtils.hdlxName(actTpName);
                                        // 资金来源	    		zjly				    出资方式	资金来源	                        zjly	资金来源
                                        String zjlyName = com.customization.unionpay.market.util.FormDataUtils.getItemName(prepaymentsDetailsFormId, "zjly", zjly);
                                        log.info("zjlyName："+zjlyName);
                                        String zjlyCode = com.customization.unionpay.market.util.FormDataUtils.czfsName(zjlyName);
                                        log.info("zjlyCode："+zjlyCode);
                                        // 【已入账金额】-【已核销金额】	yrzje-yhxje	         【已入账金额】-【已核销金额】	金额	已入账-已核销金额	      je	        已入账金额-已核销金额
                                        Double rzd = Double.valueOf(yrzje);
                                        // 【已入账金额】-【已核销金额】（负数）		         负数	                    金额	已入账-已核销金额（负数） fsje		已入账金额-已核销金额（负数）
                                        Double hxd = Double.valueOf(yhxje);
                                        // 是否为积点业务			sfwjdyw (单号查)                        过滤条件	是否为积点业务	sfwjdyw	是否为积点业务
                                        String sfwjdyw = marketingApplicationInfo.getString("sfwjdyw");
                                        // 营销事项申请单号   	 hzy	  行摘要
                                        params.put("hzy",yxsxsqdh);
                                        // 合同编码           ht        合同
                                        params.put("ht",htbh);
                                        // 供应商编码         ks1	      客商1
                                        params.put("ks1",gysbh);
                                        // 经济事项          jjsx1      经济事项1
                                        params.put("jjsx1",jjsx);
                                        // 活动类型          hdlx1  	   活动类型1
                                        params.put("hdlx1",actTpCode);
                                        // 资金来源          czfs1       出资方式1
                                        params.put("czfs1",zjlyCode);
                                        Double je = rzd - hxd;
                                        if(je > 0){
                                            // 已入账金额-已核销金额         je1 	 金额1
                                            params.put("je1",je);
                                        }else {
                                            // 已入账金额-已核销金额 (负数)    je2	 金额2
                                            params.put("je2",je);
                                        }
                                        // 财务是否结束                      zd6	 字段6
                                        params.put("zd6",cwsfjs);
                                        // 是否为大协议                 	   zd7	 字段7
                                        params.put("zd7",sfwdxy);
                                        // 是否为积点业务                     zd8   字段8
                                        params.put("zd8",sfwjdyw);
                                        detailsList.add(params);

                                    }else {
                                        isFlag = false;
                                        detailsList = new ArrayList<>();
                                        if (result != null) {
                                            result += "," + "获取期间为："+rq+"的原事项申请单号的数据为空！";
                                        } else {
                                            result = "";
                                            result += "获取期间为："+rq+"的原事项申请单号的数据为空！";
                                        }
                                        break;
                                    }
                                }
                                // TODO 冲销凭证号 cxpzh
                                log.info("mainParams:"+mainParams+",detailsList:"+detailsList);
                                log.info("isFlag:"+isFlag+",mainParams.size():"+mainParams.size()+",detailsList.size():"+detailsList.size());
                                if(isFlag && mainParams.size() > 0 && detailsList.size() > 0){
                                    int dt3 = com.customization.unionpay.market.util.FormDataUtils.addMainAndDetailsData(kjyqFormId, kjyqFormmodeId, user, kjyqTableName, "_dt3", mainParams, detailsList);
                                    if(dt3 != -1){
                                        log.info("冲销会计引擎中间表推送成功！");
                                        try {
                                            com.customization.unionpay.market.util.AccountingEngineUtils.pushAccountingEngine(String.valueOf(dt3),"未核销预付款结转台账明细台账",user);
                                            // 修改冲销日期明细表
                                            updateWriteOffByQj(rq,prepaymentsDetailsTableName,nowDate);
                                            // 修改冲销日期汇总表
                                            updateWriteOff(id,prepaymentsTableName,nowDate);
                                        }catch (Exception e){
                                            log.error("中间表推送会计引擎异常："+e.getMessage());
                                            log.error("删除中间表对应主表和明细表的数据："+dt3);
                                            com.customization.unionpay.market.util.ClearFundButtonRelated.deleteByBillId(kjyqTableName, dt3);
                                            com.customization.unionpay.market.util.ClearFundButtonRelated.deleteDetailsByBillId(kjyqTableName+"_dt3", dt3);
                                            if (result != null) {
                                                result += "," + "推送期间为："+rq+"的结转数据到中间表推送会计引擎异常："+e.getMessage();
                                            } else {
                                                result = "";
                                                result += "推送期间为："+rq+"的结转数据到中间表推送会计引擎异常："+e.getMessage();
                                            }
                                        }
                                    }else {
                                        if (result != null) {
                                            result += "," + "推送期间为："+rq+"的冲销数据到会计引擎中间表失败";
                                        } else {
                                            result = "";
                                            result += "推送期间为："+rq+"的冲销数据到会计引擎中间表失败";
                                        }
                                    }

                                }else {
                                    if (result != null) {
                                        result += "," + "获取期间为："+rq+"的未核销预付款结转明细台账数据为空";
                                    } else {
                                        result = "";
                                        result += "获取期间为："+rq+"的获取未核销预付款结转明细台账数据为空";
                                    }
                                }
                            }else {
                                if (result != null) {
                                    result += "," + "获取期间为："+rq+"的未核销预付款结转明细台账数据异常";
                                } else {
                                    result = "";
                                    result += "获取期间为："+rq+"的未核销预付款结转明细台账数据异常";
                                }
                            }
                        }
                    }else {
                        if (result != null) {
                            result += "," + "获取未核销预付款结转台账数据为空";
                        } else {
                            result = "";
                            result += "获取未核销预付款结转台账数据为空";
                        }
                    }
                }else {
                    if (result != null) {
                        result += "," + "获取未核销预付款结转明细台账为空";
                    } else {
                        result = "";
                        result += "获取未核销预付款结转明细台账为空";
                    }
                }
            }else {
                if (result != null) {
                    result += "," + "获取未核销预付款结转台账为空或者已冲销";
                } else {
                    result = "";
                    result += "获取未核销预付款结转台账为空或者已冲销";
                }
            }
        }catch (Exception e){
            if (result != null) {
                result += "," + e.getMessage();
            } else {
                result = "";
                result += e.getMessage();
            }
        }
        return result;
    }

    @Override
    public String carryoverOfUnverifiedCarryForward(String ids, User user) {
        // 结转
        String result = null;
        try {
            ResourceComInfo rci = new ResourceComInfo();
            DepartmentComInfo dci = new DepartmentComInfo();
            SubCompanyComInfo scci = new SubCompanyComInfo();

            // 未核销预付款结转台账台账表单id
            int prepaymentsFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_form_id"));
            // 未核销预付款结转台账明细台账表单id
            int prepaymentsDetailsFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_details_form_id"));


            // 会计引擎中间表表单id
            int kjyqFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "kjyq_form_id"));
            // 会计引擎中间表模块id
            int kjyqFormmodeId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "kjyq_formmode_id"));

            // # 营销事项申请流程id
            String marketingApplicationWorkId = com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_application_work_id");
            String workFlowTableName = com.customization.unionpay.market.util.FormDataUtils.getWorkFlowTableName(marketingApplicationWorkId);
            log.info("申请单主表表名：" + workFlowTableName);

            // 未核销预付款结转台账
            String prepaymentsTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(prepaymentsFormId);
            log.info("未核销预付款结转台账："+prepaymentsTableName);
            // 未核销预付款结转明细台账
            String prepaymentsDetailsTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(prepaymentsDetailsFormId);
            log.info("未核销预付款结转明细台账："+prepaymentsDetailsTableName);
            // 会计引擎中间表台账
            String kjyqTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(kjyqFormId);
            log.info("会计引擎中间表台账："+kjyqTableName);

            int userUID = user.getUID();

            if(StrUtil.isNotBlank(prepaymentsTableName)) {
                if(StrUtil.isNotBlank(prepaymentsDetailsTableName)) {
                    String useId = String.valueOf(userUID);

                    String workCode = rci.getWorkcode(useId);
                    log.info("workCode："+workCode);

                    String departmentId = rci.getDepartmentID(useId);
                    String departmentCode = dci.getDepartmentCode(departmentId);
                    log.info("departmentCode："+departmentCode);

                    String subCompanyId = rci.getSubCompanyID(useId);
                    String subCompanyCode = scci.getSubCompanyCode(subCompanyId);
                    log.info("subCompanyCode："+subCompanyCode);

                    String personId = com.customization.unionpay.market.util.FormDataUtils.getPersonByRoleName("营销-财务主管");
                    log.info("personId："+personId);

                    String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

                    List<JSONObject> carryoverDatas = CarryoverOfUnverifiedButton.selectCarryoverOfUnverifiedByIds(prepaymentsTableName, ids,"jsrq");
                    log.info("查询jsrq数据："+carryoverDatas);

                    if(carryoverDatas.size() > 0){
                        for (JSONObject carryoverData : carryoverDatas) {
                            String id = carryoverData.getString("id");
                            String rq = carryoverData.getString("rq");

                            String jzpch = UUID.randomUUID().toString();
                            log.info("结转批次号："+jzpch);

                            // 获取选择期间的明细数据
                            List<JSONObject> carryoverDetailsDatas = com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.selectCarryoverOfUnverifiedDetailsByQJ(prepaymentsDetailsTableName, rq);
                            log.info("查询rq明细数据："+carryoverDetailsDatas);
                            if(carryoverDetailsDatas.size() > 0) {
                                List<JSONObject> detailsList = new ArrayList<>();

                                JSONObject mainParams = new JSONObject();
                                mainParams.put("lydjlx","未核销预付款结转台账结转");
                                Boolean isFlag = true;

                                for (JSONObject carryoverDetailsData : carryoverDetailsDatas) {
                                    String yxsxsqdh = carryoverDetailsData.getString("yxsxsqdh");
                                    log.info("yxsxsqdh："+yxsxsqdh);
                                    // 查询营销事项申请单
                                    JSONObject marketingApplicationInfo = com.customization.unionpay.market.util.FormDataUtils.getMarketingApplicationInfo(yxsxsqdh, workFlowTableName);
                                    log.info("查询营销事项申请单数据："+marketingApplicationInfo);
                                    if(marketingApplicationInfo != null){
                                        JSONObject params = new JSONObject();

                                        String detailsId = carryoverDetailsData.getString("id");
                                        com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.updatePchById(detailsId,jzpch,"jzpch",prepaymentsDetailsTableName);

                                        String yxpthdsqm = carryoverDetailsData.getString("yxpthdsqm");

                                        String cwsfjs = carryoverDetailsData.getString("cwsfjs");
                                        String sfwdxy = carryoverDetailsData.getString("sfwdxy");
                                        String jjsx = carryoverDetailsData.getString("jjsx");
                                        String gysbh = carryoverDetailsData.getString("gysbh");
                                        String htbh = carryoverDetailsData.getString("htbh");
                                        String zjly = carryoverDetailsData.getString("zjly");
                                        String actTp = carryoverDetailsData.getString("ACT_TP");
                                        String yrzje = carryoverDetailsData.getString("yrzje");
                                        String yhxje = carryoverDetailsData.getString("yhxje");

                                        // 核算主体			    (单号查)   zth          法人公司	        核算主体编码	hsztbm	核算主体
                                        String zth = marketingApplicationInfo.getString("zth");

                                        Object hszt = mainParams.get("hszt");
                                        if(hszt == null  || "".equals(hszt)){
                                            mainParams.put("hszt",zth);
                                        }
                                        // rzrqDate 结算日期	     		jsrq		           凭证日期、头文本	入账日期	    rzrq	入账日期
                                        Object rzrq = mainParams.get("rzrq");
                                        if(rzrq == null  || "".equals(rzrq)){
                                            mainParams.put("rzrq",nowDate);
                                        }
                                        // jzpzh 结转批次号	jzpzh		           备选参考编号	    表单号	    bdh	    表单号
                                        Object bdh = mainParams.get("bdh");
                                        if(bdh == null  || "".equals(bdh)){
                                            mainParams.put("bdh",jzpch);
                                        }
                                        // useId          凭证制单人				触发【核算入账】的用户ID	凭证制单人	    凭证制单人	zhidanren	凭证制单人
                                        Object pzzdr = mainParams.get("pzzdr");
                                        if(pzzdr == null  || "".equals(pzzdr)){
                                            mainParams.put("pzzdr",useId);
                                        }
                                        // personId       凭证复核人				财务主管	                凭证复核人	    凭证复核人	fuheren	    凭证复核人
                                        Object pzfhr = mainParams.get("pzfhr");
                                        if(pzfhr == null  || "".equals(pzfhr)){
                                            mainParams.put("pzfhr",personId);
                                        }
                                        // workCode       经办人					       经办人（不传浪潮，返回到表单凭证明细）	    经办人	    jbr	    经办人
                                        Object jbr = mainParams.get("jbr");
                                        if(jbr == null  || "".equals(jbr)){
                                            mainParams.put("jbr",useId);
                                        }
                                        // departmentCode 部门	        		 		   经办人部门（不传浪潮，返回到表单凭证明细）	经办人部门	jbrbm	经办人部门
                                        Object jbrbm = mainParams.get("jbrbm");
                                        if(jbrbm == null  || "".equals(jbrbm)){
                                            mainParams.put("jbrbm",departmentId);
                                        }
                                        // subCompanyCode 公司	        		 		   经办人公司（不传浪潮，返回到表单凭证明细）	经办人公司	jbrgs	经办人公司
                                        Object jbrgs = mainParams.get("jbrgs");
                                        if(jbrgs == null  || "".equals(jbrgs)){
                                            mainParams.put("jbrgs",subCompanyId);
                                        }

                                        // TODO 经济事项编码
                                        // 经济事项	    		jjsx 					取编码	映射表	经济事项编码	            jjsxbm	经济事项
//                                        JSONObject economicMattersInfo = FormDataUtils.getEconomicMattersInfo(jjsx);
//                                        String economicCode = economicMattersInfo.getString("code");
                                        // 活动类型	    		ACT_TP（查表）					取编码	活动类型、映射表字段	活动类型编码	hdlxbm	活动类型
                                        String actTpName = com.customization.unionpay.market.util.FormDataUtils.getItemName(prepaymentsDetailsFormId, "ACT_TP", actTp);
                                        String actTpCode = com.customization.unionpay.market.util.FormDataUtils.hdlxName(actTpName);
                                        // 资金来源	    		zjly				    出资方式	资金来源	                        zjly	资金来源
                                        String zjlyName = com.customization.unionpay.market.util.FormDataUtils.getItemName(prepaymentsDetailsFormId, "zjly", zjly);
                                        log.info("zjlyName："+zjlyName);
                                        String zjlyCode = com.customization.unionpay.market.util.FormDataUtils.czfsName(zjlyName);
                                        log.info("zjlyCode："+zjlyCode);
                                        // 【已入账金额】-【已核销金额】	yrzje-yhxje	         【已入账金额】-【已核销金额】	金额	已入账-已核销金额	      je	        已入账金额-已核销金额
                                        Double rzd = Double.valueOf(yrzje);
                                        // 【已入账金额】-【已核销金额】（负数）		         负数	                    金额	已入账-已核销金额（负数） fsje		已入账金额-已核销金额（负数）
                                        Double hxd = Double.valueOf(yhxje);
                                        // 是否为积点业务			sfwjdyw (单号查)                        过滤条件	是否为积点业务	sfwjdyw	是否为积点业务
                                        String sfwjdyw = marketingApplicationInfo.getString("sfwjdyw");
                                        // 营销事项申请单号   	 hzy	  行摘要
                                        params.put("hzy",yxsxsqdh);
                                        // 合同编码           ht        合同
                                        params.put("ht",htbh);
                                        // 供应商编码         ks1	      客商1
                                        params.put("ks1",gysbh);
                                        // 经济事项          jjsx1      经济事项1
                                        params.put("jjsx1",jjsx);
                                        // 活动类型          hdlx1  	   活动类型1
                                        params.put("hdlx1",actTpCode);
                                        // 资金来源          czfs1       出资方式1
                                        params.put("czfs1",zjlyCode);
                                        Double je = rzd - hxd;
                                        if(je > 0){
                                            // 已入账金额-已核销金额         je1 	 金额1
                                            params.put("je1",je);
                                        }else {
                                            // 已入账金额-已核销金额 (负数)    je2	 金额2
                                            params.put("je2",je);
                                        }
                                        // 财务是否结束                      zd6	 字段6
                                        params.put("zd6",cwsfjs);
                                        // 是否为大协议                 	   zd7	 字段7
                                        params.put("zd7",sfwdxy);
                                        // 是否为积点业务                     zd8   字段8
                                        params.put("zd8",sfwjdyw);

                                        detailsList.add(params);

                                    }else {
                                        isFlag = false;
                                        if (result != null) {
                                            result += "," + "获取期间为："+rq+"的原事项申请单号："+yxsxsqdh+"的数据为空";
                                        } else {
                                            result = "";
                                            result += "获取期间为："+rq+"的原事项申请单号："+yxsxsqdh+"的数据为空";
                                        }
                                        break;
                                    }
                                }
                                // TODO 结转凭证号 jzpzh
                                log.info("mainParams:"+mainParams+",detailsList:"+detailsList);
                                log.info("isFlag:"+isFlag+",mainParams.size():"+mainParams.size()+",detailsList.size():"+detailsList.size());
                                if(isFlag && mainParams.size() > 0 && detailsList.size() > 0){
                                    int dt3 = com.customization.unionpay.market.util.FormDataUtils.addMainAndDetailsData(kjyqFormId, kjyqFormmodeId, user, kjyqTableName, "_dt3", mainParams, detailsList);
                                    if(dt3 != -1){
                                        log.info("冲销会计引擎中间表推送成功！");
                                        try {
                                            com.customization.unionpay.market.util.AccountingEngineUtils.pushAccountingEngine(String.valueOf(dt3),"未核销预付款结转台账明细台账",user);
                                            // 修改结转日期明细表
                                            updateCarryForwardQj(rq,prepaymentsDetailsTableName,nowDate);
                                            // 修改冲销日期汇总表
                                            updateCarryForward(id,prepaymentsTableName,nowDate);
                                        }catch (Exception e){
                                            log.error("中间表推送会计引擎异常："+e.getMessage());
                                            log.error("删除中间表对应主表和明细表的数据："+dt3);
                                            com.customization.unionpay.market.util.ClearFundButtonRelated.deleteByBillId(kjyqTableName, dt3);
                                            com.customization.unionpay.market.util.ClearFundButtonRelated.deleteDetailsByBillId(kjyqTableName+"_dt3", dt3);
                                            if (result != null) {
                                                result += "," + "推送期间为："+rq+"的结转数据到中间表推送会计引擎异常："+e.getMessage();
                                            } else {
                                                result = "";
                                                result += "推送期间为："+rq+"的结转数据到中间表推送会计引擎异常："+e.getMessage();
                                            }
                                        }
                                    }else {
                                        if (result != null) {
                                            result += "," + "推送期间为："+rq+"的结转数据到会计引擎中间表失败";
                                        } else {
                                            result = "";
                                            result += "推送期间为："+rq+"的结转数据到会计引擎中间表失败";
                                        }
                                    }
                                }else {
                                    if (result != null) {
                                        result += "," + "获取期间为："+rq+"的未核销预付款结转明细台账数据为空";
                                    } else {
                                        result = "";
                                        result += "获取期间为："+rq+"的获取未核销预付款结转明细台账数据为空";
                                    }
                                }
                            }else {
                                if (result != null) {
                                    result += "," + "获取期间为："+rq+"的未核销预付款结转明细台账数据为空";
                                } else {
                                    result = "";
                                    result += "获取期间为："+rq+"的获取未核销预付款结转明细台账数据为空";
                                }
                            }
                        }
                    }else {
                        if (result != null) {
                            result += "," + "获取未核销预付款结转台账数据为空";
                        } else {
                            result = "";
                            result += "获取未核销预付款结转台账数据为空";
                        }
                    }
                }else {
                    if (result != null) {
                        result += "," + "获取未核销预付款结转明细台账为空";
                    } else {
                        result = "";
                        result += "获取未核销预付款结转明细台账为空";
                    }
                }
            }else {
                if (result != null) {
                    result += "," + "获取未核销预付款结转台账为空或者已结转";
                } else {
                    result = "";
                    result += "获取未核销预付款结转台账为空或者已结转";
                }
            }
        }catch (Exception e){
            if (result != null) {
                result += "," + e.getMessage();
            } else {
                result = "";
                result += e.getMessage();
            }
        }
        return result;
    }

    /**
     * 修改冲销时间
     * @param id
     * @param tableName
     * @param date
     */
    private void updateWriteOff(String id,String tableName,String date){
        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        try {
            String updateSql = "update #{tableName} set cxrq=? where id = ? ";
            updateSql = updateSql.replace("#{tableName}",tableName);
            log.info("updateSql:"+updateSql);
            rst.executeUpdate(updateSql,date,id);
            // 提交事务
            rst.commit();
        } catch (Exception e) {
            // 回滚
            rst.rollback();
            log.error("修改数据异常：" + e.getMessage());
        }
    }

    /**
     * 修改结转时间
     * @param id
     * @param tableName
     * @param date
     */
    private void updateCarryForward(String id,String tableName,String date){
        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        try {
            String updateSql = "update #{tableName} set  jsrq=? where id = ? ";
            updateSql = updateSql.replace("#{tableName}",tableName);
            log.info("updateSql:"+updateSql);
            rst.executeUpdate(updateSql,date,id);
            // 提交事务
            rst.commit();
        } catch (Exception e) {
            // 回滚
            rst.rollback();
            log.error("修改数据异常：" + e.getMessage());
        }
    }


    /**
     * 修改明细冲销时间
     * @param rq
     * @param tableName
     * @param date
     */
    private void updateWriteOffByQj(String rq,String tableName,String date){
        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        try {
            String updateSql = "update #{tableName} set cxrq=? where qj=? ";
            updateSql = updateSql.replace("#{tableName}",tableName);
            log.info("updateSql:"+updateSql);
            rst.executeUpdate(updateSql,date,rq);
            // 提交事务
            rst.commit();
        } catch (Exception e) {
            // 回滚
            rst.rollback();
            log.error("修改数据异常：" + e.getMessage());
        }
    }

    /**
     * 修改明细结转时间
     * @param rq
     * @param tableName
     * @param date
     */
    private void updateCarryForwardQj(String rq,String tableName,String date){
        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        try {
            String updateSql = "update #{tableName} set  jsrq=? where qj=? ";
            updateSql = updateSql.replace("#{tableName}",tableName);
            log.info("updateSql:"+updateSql);
            rst.executeUpdate(updateSql,date,rq);
            // 提交事务
            rst.commit();
        } catch (Exception e) {
            // 回滚
            rst.rollback();
            log.error("修改数据异常：" + e.getMessage());
        }
    }

    /**
     *
     * @param singleCode
     * @param ysxsqd
     * @param bchxje 供应商编号 营销事项申请单号 本次核销金额 经济事项 营销平台活动授权码	 财务是否结束	 公司  部门 是否为大协议 合同编号 活动类型 资金来源 已入账金额  入账批次号
     * @return
     */
    private JSONObject chooseDataByOnlyValue(JSONObject singleCode, String ysxsqd,Double bchxje,String gysbh,String jjsx,String yxpthdsqm,String cwsfjs,String gs,String  bm,String sfwdxy,String htbh,String actTp,String zjly,Double yrzje,String rzpch){

        // 唯一值
        Object rjson = singleCode.get(ysxsqd);
        if (rjson != null) {
            JSONObject singleData = singleCode.getJSONObject(ysxsqd);
            Double bchxjeD = singleData.getDouble("bchxje") + bchxje;
            Double yrzjeD = 0D;
            if(singleData.getDouble("yrzje") == null){
                yrzjeD = yrzje;
            }else {
                yrzjeD = singleData.getDouble("yrzje") + yrzje;
            }
            singleData.put("bchxje", bchxjeD);
            singleData.put("yrzje", yrzjeD);
            if("".equals(singleData.getString("yxpthdsqm"))){
                singleData.put("yxpthdsqm", yxpthdsqm);
            }
            if("".equals(singleData.getString("cwsfjs"))){
                singleData.put("cwsfjs", cwsfjs);
            }
            if("".equals(singleData.getString("gs"))){
                singleData.put("gs", gs);
            }
            if("".equals(singleData.getString("bm"))){
                singleData.put("bm", bm);
            }
            if("".equals(singleData.getString("sfwdxy"))){
                singleData.put("sfwdxy", sfwdxy);
            }
            if("".equals(singleData.getString("htbh"))){
                singleData.put("htbh", htbh);
            }
            if("".equals(singleData.getString("actTp"))){
                singleData.put("actTp", actTp);
            }
            if("".equals(singleData.getString("zjly"))){
                singleData.put("zjly", zjly);
            }
            if("".equals(singleData.getString("rzpch"))){
                singleData.put("rzpch", rzpch);
            }

            singleData.put(ysxsqd, singleData);
        } else {
            JSONObject singleData = new JSONObject();
            singleData.put("bchxje", bchxje);
            singleData.put("gysbh", gysbh);
            singleData.put("jjsx", jjsx);
            singleData.put("yxpthdsqm", yxpthdsqm);
            singleData.put("cwsfjs", cwsfjs);
            singleData.put("gs", gs);
            singleData.put("bm", bm);
            singleData.put("sfwdxy", sfwdxy);
            singleData.put("htbh", htbh);
            singleData.put("actTp", actTp);
            singleData.put("zjly", zjly);
            singleData.put("yrzje", yrzje);
            singleData.put("rzpch", rzpch);
            singleCode.put(ysxsqd, singleData);
        }

        return singleCode;
    }
}
